using System;
using System.Collections.Generic;
using System.Linq;

namespace EOSDigital.API
{
	public abstract class ValueBase
	{
		protected sealed class CameraValueComparer : IEqualityComparer<CameraValue>
		{
			public bool Equals(CameraValue x, CameraValue y)
			{
				return x.Equals(y);
			}

			public int GetHashCode(CameraValue obj)
			{
				return obj.GetHashCode();
			}
		}

		public static CameraValue GetValue(int value, List<CameraValue> Values)
		{
			CameraValue[] array = (from t in Values
			where t.IntValue == value
			select t).ToArray();
			if (array.Length == 0)
			{
				CameraValue cameraValue = Values.FirstOrDefault((CameraValue t) => t.IntValue == -1);
				if (cameraValue != (CameraValue)null)
				{
					return cameraValue;
				}
				throw new KeyNotFoundException("There is no CameraValue for this ID");
			}
			return array[0];
		}

		public static CameraValue GetValue(string value, List<CameraValue> Values)
		{
			CameraValue[] array = (from t in Values
			where t.StringValue == value
			select t).ToArray();
			if (array.Length == 0)
			{
				CameraValue cameraValue = Values.FirstOrDefault((CameraValue t) => t.IntValue == -1);
				if (cameraValue != (CameraValue)null)
				{
					return cameraValue;
				}
				throw new KeyNotFoundException("There is no CameraValue for this ID");
			}
			return array[0];
		}

		public static CameraValue GetValue(double value, List<CameraValue> Values)
		{
			CameraValue[] array = (from t in Values.Distinct(new CameraValueComparer()).Where(delegate(CameraValue t)
			{
				if (t.IntValue != -1 && t != TvValues.Bulb)
				{
					return t != ISOValues.Auto;
				}
				return false;
			})
			orderby t.DoubleValue
			select t).ToArray();
			for (int i = 0; i < array.Length; i++)
			{
				if (Math.Abs(array[i].DoubleValue - value) <= 1E-11)
				{
					return array[i];
				}
				if (array[i].DoubleValue > value)
				{
					if (i == 0)
					{
						return array[i];
					}
					double num = value - array[i - 1].DoubleValue;
					double num2 = array[i].DoubleValue - value;
					if (num2 > num)
					{
						return array[i - 1];
					}
					return array[i];
				}
			}
			return array[array.Length - 1];
		}
	}
}
